import java.security.Key;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap.KeySetView;
import javax.jws.soap.SOAPBinding.Use;
import javax.swing.KeyStroke;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/11 9:07
 * @description: 接口实现
 */
public class UserDaoMap implements UserDao {
  HashMap<String,User> map=new HashMap<>();
  @Override
  public void register(User user) {
    map.put(user.getName(),user);
  }

  @Override
  public boolean login(String name, String password) {
    //遍历集合：将传入的name和password跟集合元素比较，
    //          若相同，返回true
    //          若不同，返回false
    Iterator iterator=map.keySet().iterator();
    while (iterator.hasNext()){
      String key=(String) iterator.next();
      User value=map.get(key);
      if (name.equals(key)&&password.equals(value.getPassword())){
        return true;
      }
    }
    return false;
  }

  @Override
  public void queryAll() {
    //遍历集合：输出内容
    System.out.println("所有用户信息如下：");
    Iterator iterator=map.entrySet().iterator();
    while (iterator.hasNext()){
      Map.Entry<String,User> entry=(Map.Entry<String,User>) iterator.next();
      User value= entry.getValue();
      System.out.println(value);
    }

  }

  /*HashMap<String,User>map = new HashMap<>();
  @Override
  public void register(User user) {
    map.put(user.getName(),user);
  }

  @Override
  public boolean login(String name, String password) {
    //遍历集合：将传入的name和password跟集合元素比较，
    //          若相同，返回true
    //          若不同，返回false
    Iterator iterator=map.keySet().iterator();
    while (iterator.hasNext()){
      String key=(String) iterator.next();
      User value=map.get(key);
      if (name.equals(key)&&password.equals(value.getPassword())){
        return true;
      }
    }
    return false;
  }

  @Override
  public void queryAll() {
//遍历集合：输出内容
    System.out.println("所有信息如下：");
    Iterator iterator=map.entrySet().iterator();
    while (iterator.hasNext()){
      Map.Entry<String,User>  entry=(Map.Entry<String,User>) iterator.next();
      User value=entry.getValue();
      System.out.println(value);
    }
  }*/
}
